package com.atguigu.gmall.realtime.app.dwm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.app.BaseAppV2;
import com.atguigu.gmall.realtime.bean.*;
import com.atguigu.gmall.realtime.common.Constant;
import com.atguigu.gmall.realtime.util.AtguiguUtil;
import com.atguigu.gmall.realtime.util.DimUtil;
import com.atguigu.gmall.realtime.util.FlinkSinkUtil;
import com.atguigu.gmall.realtime.util.JDBCUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.sql.Connection;
import java.time.Duration;
import java.util.Map;

import static com.atguigu.gmall.realtime.common.Constant.*;

/**
 * 订单宽表
 * @author Archie
 * @date 2021-10-21 14:53
 * @description
 */
public class DwmPaymentWide extends BaseAppV2 {

	public static void main(String[] args) {
		new DwmPaymentWide().init(
				3004,
				1,
				"DwmPaymentWide",
				"DwmPaymentWide",
				TOPIC_DWM_ORDER_WIDE, TOPIC_DWD_PAYMENT_INFO);
	}

	@Override
	protected void run(StreamExecutionEnvironment env,
					   Map<String, DataStreamSource<String>> streams) {
		/*streams.get(TOPIC_DWM_ORDER_WIDE).print(TOPIC_DWM_ORDER_WIDE);
		streams.get(TOPIC_DWD_PAYMENT_INFO).print(TOPIC_DWD_PAYMENT_INFO);*/

		KeyedStream<PaymentInfo, Long> paymentInfoStream = streams.get(TOPIC_DWM_ORDER_WIDE)
				.map(data -> JSON.parseObject(data, PaymentInfo.class))
				.assignTimestampsAndWatermarks(
						WatermarkStrategy
								.<PaymentInfo>forBoundedOutOfOrderness(Duration.ofSeconds(3))
								.withTimestampAssigner((data, ts) -> AtguiguUtil.toTs(data.getCreate_time()))
				)
				.keyBy(PaymentInfo::getOrder_id);

		KeyedStream<OrderWide, Long> orderWideStream = streams.get(TOPIC_DWD_PAYMENT_INFO)
				.map(data -> JSON.parseObject(data, OrderWide.class))
				.assignTimestampsAndWatermarks(
						WatermarkStrategy
								.<OrderWide>forBoundedOutOfOrderness(Duration.ofSeconds(3))
								.withTimestampAssigner((data, ts) -> AtguiguUtil.toTs(data.getCreate_time()))
				)
				.keyBy(OrderWide::getOrder_id);

		// 1. 双流JOIN
		paymentInfoStream.intervalJoin(orderWideStream)
				.between(Time.minutes(-45), Time.seconds(10))
				.process(new ProcessJoinFunction<PaymentInfo, OrderWide, PaymentWide>() {
					@Override
					public void processElement(PaymentInfo left,
											   OrderWide right,
											   Context ctx,
											   Collector<PaymentWide> out) throws Exception {
						out.collect(new PaymentWide(left, right));
					}
				})
				.map(JSON::toJSONString)
				//2. 写出到Kafka
				.addSink(FlinkSinkUtil.getKafkaSink(Constant.TOPIC_DWM_PAYMENT_WIDE));
	}

}
